package com.mars.diancan.controller;

import com.google.common.collect.Maps;
import com.mars.diancan.VO.ResultVO;
import com.mars.diancan.common.ResultEnum;
import com.mars.diancan.converter.OrderForm2OrderDTOConverter;
import com.mars.diancan.dto.OrderDTO;
import com.mars.diancan.exception.DiancanException;
import com.mars.diancan.form.OrderForm;
import com.mars.diancan.pojo.OrderMaster;
import com.mars.diancan.service.BuyerService;
import com.mars.diancan.service.OrderService;
import com.mars.diancan.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author MARS
 * @date 2018/6/26
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     *
     * @param orderForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm,BindingResult bindingResult) {

        // 判断表单单数是否正确
        // 获取错误实体属性信息
        if (bindingResult.hasErrors()) {
            log.error("创建订单，参数不正确,orderForm={}",orderForm);
            throw new DiancanException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        // 将orderForm转化为orderDTO
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.converter(orderForm);
        // 转换之后，再次判断一下，购物车是否为空
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("创建订单，购物车不能为空");
            throw new DiancanException(ResultEnum.CART_EMPTY);
        }
        OrderDTO result = orderService.create(orderDTO);
        Map<String,String> map = Maps.newHashMap();
        map.put("orderId",result.getOrderId());
        return ResultVOUtil.success(map);
    }

    /**
     * 订单列表
     * @param openid
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size",defaultValue = "2") Integer size) {
        // 校验openid
        if (StringUtils.isEmpty(openid)) {
            log.error("查询订单列表，openid为空");
            throw new DiancanException(ResultEnum.PARAM_ERROR);
        }

        // 保证程序健壮性，可以给page设置默认值
        PageRequest pageRequest = new PageRequest(page,size);
        Page<OrderDTO> orderDTOPage =  orderService.findList(openid,pageRequest);
        // 从Page中获取数据的总数，内容....

        log.info("getTotalElements:查询列表的结果-->{}",orderDTOPage.getTotalElements());
        log.info("getTotalPages:查询结果的页数-->{}",orderDTOPage.getTotalPages());
        log.info("getContent:查询结果的内容-->{}",orderDTOPage.getContent());
        log.info("getSize:一页查询几条-->{}",orderDTOPage.getSize());

        return ResultVOUtil.success(orderDTOPage.getContent());
    }

    /**
     * 获取订单详情
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail (@RequestParam("openid") String openid,
                            @RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = buyerService.findOrderOne(openid,orderId);
        return ResultVOUtil.success(orderDTO);
    }


    /**
     * 取消订单
     * @param openid
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {
        buyerService.cancelOrder(openid,orderId);
        return ResultVOUtil.success();
    }

}
